package com.dirk.happie.activities;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dirk.happie.R;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Common;
import com.dirk.happie.utils.Keys;
import com.dirk.happie.utils.LogoDownloader;

public class ThankYouActivity extends Activity {
	private LinearLayout layoutTop;
	private int from;
	private float totalPrice;
	private App mApp;
	private LogoDownloader logoDownloader = null;
	private String cacheFolderPath = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thank_you);
		this.mApp = ((App) getApplication());
		from = getIntent().getIntExtra("From", 0);

		ImageView companyLogoView = (ImageView) findViewById(R.id.img_company);
		cacheFolderPath = Environment.getExternalStorageDirectory().getPath() + "/happie_cache";
		File file = new File(cacheFolderPath);
		file.mkdirs();
		logoDownloader = new LogoDownloader();
		if (mApp.getCompanyLogo() != null) {
			logoDownloader.download(Keys.LOGOS_URL + mApp.getCompanyLogo(), cacheFolderPath + "/" + Common.getMD5(mApp.getCompanyLogo()), companyLogoView);
		}
		createView();

		footerMenuListeners();
	}

	private void createView() {

		layoutTop = (LinearLayout) findViewById(R.id.linearTop);
		
		RelativeLayout relDeliveryCost = (RelativeLayout) findViewById(R.id.relativeDeliveryCost);
		TextView deliveryCost = (TextView) findViewById(R.id.cost);

		createProductLayouts();

		TextView priceTotal = (TextView) findViewById(R.id.priceTotal);
		
		if (mApp.getSettings().getMinOrderValue() != null && !mApp.getSettings().getMinOrderValue().equals("") && from ==1) {
			if (totalPrice >= Float.parseFloat(Common.getPrice(mApp.getSettings().getMinOrderValue()))) {
				priceTotal.setText(getString(R.string.currency) + " " + Common.getPrice(Float.toString(totalPrice)));
				relDeliveryCost.setVisibility(View.GONE);
			} else {
				priceTotal.setText(getString(R.string.currency) + " " + Common.getPrice(Float.toString(totalPrice + Float.parseFloat(Common.getPrice(mApp.getSettings().getDeliveryCost())))));
				relDeliveryCost.setVisibility(View.VISIBLE);
				deliveryCost.setText(getString(R.string.currency) + " " + Common.getPrice(mApp.getSettings().getDeliveryCost()));
			}
		} else {
			priceTotal.setText(getString(R.string.currency) + " " + Common.getPrice(Float.toString(totalPrice)));
		}
//		priceTotal.setText(getString(R.string.currency) + " " + Common.getPrice(Float.toString(totalPrice)));

		createPickupTimeLayout();

	}

	private void createProductLayouts() {
		for (int i = 0; i < mApp.getBasketList().size(); i++) {
			RelativeLayout relativeLayout = new RelativeLayout(this);
			relativeLayout.setPadding(5, 5, 5, 5);
			// layout.setGravity(Gravity.FILL_HORIZONTAL);
			totalPrice += Integer.parseInt(mApp.getBasketList().get(i).getCount()) * Float.parseFloat(Common.getPrice(mApp.getBasketList().get(i).getPrice()));
			LinearLayout linearLayout = new LinearLayout(this);
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);

			TextView textCount = new TextView(this);
			textCount.setPadding(5, 5, 5, 5);
			textCount.setText(mApp.getBasketList().get(i).getCount());
			textCount.setId(i);
			textCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
			textCount.setGravity(Gravity.LEFT);

			linearLayout.addView(textCount);

			TextView textTitle = new TextView(this);
			textTitle.setPadding(5, 5, 5, 5);
			if (mApp.getBasketList().get(i).getIdExtra() == null) {
				textTitle.setText(mApp.getBasketList().get(i).getName() + "\n" + mApp.getBasketList().get(i).getSize());
			} else {
				textTitle.setText(mApp.getBasketList().get(i).getName());
			}
			textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
			textTitle.setGravity(Gravity.LEFT);

			linearLayout.addView(textTitle);

			relativeLayout.addView(linearLayout);

			RelativeLayout.LayoutParams paramsPrice = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
			paramsPrice.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			paramsPrice.addRule(RelativeLayout.CENTER_VERTICAL);
			TextView textPrice = new TextView(this);
			textPrice.setText(getString(R.string.currency) + " " + Common.getPrice(mApp.getBasketList().get(i).getPrice()));
			textPrice.setGravity(Gravity.RIGHT);
			textPrice.setLayoutParams(paramsPrice);
			textPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

			relativeLayout.addView(textPrice);

			layoutTop.addView(relativeLayout);

		}
	}

	private void createPickupTimeLayout() {

		TextView text = (TextView) findViewById(R.id.textPicup);

		TextView date = (TextView) findViewById(R.id.textPicupDate);
		date.setText(getIntent().getStringExtra("Date"));

		TextView time = (TextView) findViewById(R.id.textPicupTime);
		time.setText(getIntent().getStringExtra("Time"));

		if (from == 1) {
			RelativeLayout relativeAddress = (RelativeLayout) findViewById(R.id.relativeAddress);
			relativeAddress.setVisibility(View.VISIBLE);

			TextView textAddress = (TextView) findViewById(R.id.address);
			textAddress.setText(getIntent().getStringExtra("Address"));

			text.setText(R.string.text_delivery_time);
		} else {
			text.setText(R.string.text_pickup_time);
		}
	}

	private void footerMenuListeners() {

		View homeButton = findViewById(R.id.home);
		homeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(100);
				finish();
			}
		});

		View categoriesButton = findViewById(R.id.categories);
		categoriesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(200);
				finish();
			}
		});

		View basketButton = findViewById(R.id.basket);
		basketButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(300);
				finish();
			}
		});

		View searchButton = findViewById(R.id.search);
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

	}

	@Override
	protected void onDestroy() {
		if (logoDownloader != null) {
			logoDownloader.clearImages();
		}
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_thank_you, menu);
		return true;
	}

}
